package callnumber.gtdev5.com.photoVault.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.junruy.fakecall.KeepLiveAidlInterface;

import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.activity.SplashActiviy;
import callnumber.gtdev5.com.photoVault.utils.ServiceCheckUtils;


/********************
 *  Created by ：  Zeoy 
 *         Date：  2018\3\29 0029.
 * Introduction：   
 *********************/

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationListenerService extends Service {
    private IBinder binder;
    private ServiceConnection connection;
    private Intent intent;


   /* @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        startService(intent);
        bindService(intent,connection,BIND_IMPORTANT);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        startService(intent);
        bindService(intent,connection,BIND_IMPORTANT);
    }*/

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("zeoy","【广播监听服务重新开启】");
        if (!ServiceCheckUtils.isServiceWorked(this,"callnumber.gtdev5.com.fakecall_4.service.LockService")){
            Log.e("zeoy", "【通知   -  启动  -  前台】");
            startService(this.intent);
        }
        Log.e("zeoy", "【通知绑定主锁服务】");
        bindService(this.intent,connection,BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        InitNotification();
        intent = new Intent(this,LockService.class);
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.e("zeoy", "【通知绑定主锁服务成功】");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.e("zeoy", "【通知   -  断开连接前台  -  启动前台】");
                startService(intent);
                bindService(intent,connection,BIND_IMPORTANT);
            }
        };

        binder = new KeepLiveAidlInterface.Stub.Stub() {
            @Override
            public String getServiceName() throws RemoteException {
                return "NotificationListenerService";
            }
        };
        startService(intent);
    }

    private void InitNotification() {
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle(getResources().getString( R.string.app_name))
                .setChannelId("fakeCall_4")
                .setContentText("点击可进入模拟来电")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setWhen(System.currentTimeMillis())
                .build();

        Intent intent = new Intent(this, SplashActiviy.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        //设置通知默认效果
        notification.contentIntent = pendingIntent;
        notification.flags = Notification.FLAG_SHOW_LIGHTS;
        startForeground(1, notification);
    }
    @Override
    public void onDestroy() {
        if (Build.VERSION.SDK_INT>=18)
            unbindService(connection);

        stopForeground(true);
        super.onDestroy();
        //启动自己
        startService(new Intent(this,NotificationListenerService.class));
        bindService(intent,connection,BIND_IMPORTANT);
    }
}
